//
//  BTRestPwdVC.m
//  BaxTer
//
//  Created by 陈武琦 on 2016/10/26.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTModifyPwdVC.h"
#import "WCTimer.h"
#import "NSString+Utilities.h"


static __weak WCTimer *timerFgt;
@interface BTModifyPwdVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@end

@implementation BTModifyPwdVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    // Do any additional setup after loading the view from its nib.
    self.pwdTextField.delegate = self;
    self.codeBtn.layer.borderColor = [UIColor clearColor].CGColor;
    self.codeBtn.layer.cornerRadius = 5.0;
    self.codeBtn.layer.borderWidth = 0.5;
    
    //    timerFgt.securityCode = self.codeBtn;
}

#pragma mark - 倒计时

- (void)countDown
{
    WCTimer *timer = [[WCTimer alloc] init];
    __block __weak typeof(WCTimer *)weakTimer = timer;
    timer.count = 60;
    timer.securityCode = self.codeBtn;
    timer.timeEnd = ^{
        weakTimer = nil;
    };
    [timer start:1];
    
    timerFgt = timer;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark - Notification

- (void)keyboardWillHide:(NSNotification *)note
{
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformIdentity;
                     } completion:NULL];
}

#pragma mark - Nib Target

//发送验证码
- (IBAction)codeBtnTap:(id)sender {
    self.codeBtn.enabled = NO;
        cwqHud;
        
        [[BTNetworkManager sharedMg] POST:BTgetModifyPasswordVerifyCodeUrl parameters:@{} success:^(id responseObject) {
            
            //倒计时
            if ([responseObject[@"success"] boolValue]) {
                
                [self countDown];
                cwqHudSuccess;
            }else {
                cwqHudRequestError(responseObject[@"err"]);
            }
            self.codeBtn.enabled = YES;
        } failure:^(NSString *err) {
            self.codeBtn.enabled = YES;
            
        }];
}


- (IBAction)bgTouchDown:(id)sender {
    [self.view endEditing:YES];
}


- (IBAction)doSubmit:(id)sender {

    NSString *code = self.codeTextField.text;
    NSString *password = self.pwdTextField.text;
    if ([code isEmpty]) {
        alertMessage(@"请输入验证码");
        return;
    }
    
    if ([password isEmpty]) {
        alertMessage(@"请输入新密码");
        return;
    }
    
    if ([password hasSpace]) {
        alertMessage(@"密码不能有空格");
        return;
    }
    
    if (![password isPassword]) {
        alertMessage(@"密码长度为6-16位");
        return;
    }
    

    cwqHud;

    NSDictionary *param = @{@"newPassword":password, @"code":code};
    [[BTNetworkManager sharedMg] POST:BTModifyPasswordUrl parameters:param success:^(id responseObject) {
        cwqAlertMessage(@"修改成功");
        [HELAppManager updatePassword:password];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSString *err) {
    }];
}


#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIView *mustShow;
    if (textField.tag == 0) {
        mustShow = self.codeTextField;
    } else if (textField.tag == 2) {
        mustShow = self.pwdTextField;
    }
    
    CGRect rect = [mustShow convertRect:mustShow.bounds toView:self.view];
    CGFloat y = self.view.frame.size.height - rect.origin.y - rect.size.height - kKeyboardHeight;
    
#warning 为了配合键盘弹出的动画，使用7<<16.
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformMakeTranslation(0, y>=0?0:y-10);
                     } completion:NULL];
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 2) {
        [textField resignFirstResponder];
        [self doSubmit:nil];
        return YES;
    }
    
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
